#!/usr/bin/env python

""" GAEO View module. 

Classes:
    View: Base view interface.
    DjangoTemplateView: 
"""

import os

# gaeo imports
from gaeo import utils

# App imports
import settings

class View(object):
    """ 
    Base view class
    """
    
    def __init__(self, controller):
        self._controller = controller
        
    def render(self):
        raise NotImplementedError('The render method has not been implemented.')
    
    
class AppengineTemplateView(View):
    
    """ This view is implemented by using appengine's template. """
    
    __view_options = {
        'folder': '',
        'script': '',
        'ext': '.html'
    }
    
    def __init__(self, controller, template_path=settings.TEMPLATE_PATH):
        super(AppengineTemplateView, self).__init__(controller)
        self._template_path = template_path
        
    def render(self, data=None, **kwds):
        from google.appengine.ext.webapp import template
        
        # register filters
        template.register_template_library('gaeo.view.filters')
        
        # merge
        response_data = data if data else {}
        response_data.update(self.__dict__)
        params = self._controller.params
        
        opt = self.__view_options
        
        folder = utils.select_trusy(opt['folder'], params['controller'])
        script = utils.select_trusy(opt['script'], params['action'])
        
        path = os.path.join(self._template_path, 
                            folder,
                            script + opt['ext'])
        self._controller.response.out.write(template.render(path, response_data))
    
    def set_render_path(self, folder='', script='', ext='.html'):
        self.options.update({
            'folder': utils.select_trusy(folder, self.options['folder']),
            'script': utils.select_trusy(script, self.options['script']),
            'ext': utils.select_trusy(ext, self.options['ext'])
        })
    
    @property
    def options(self):
        return self.__view_options
